﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SIOCUR.Models;

namespace SIOCUR.Controllers
{
    public class RastreoLoteController : Controller
    {

        SIOCURRepository dbpRepository = new SIOCURRepository();

        //
        // GET: /RastreoLote/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Registra()
        {
            return View("Registra");
        }

        [HttpPost]
        public ActionResult Registra(FormCollection formValues, RASTREO_DE_LOTE rlot)
        {

            if (ModelState.IsValid)
            {
                dbpRepository.AddRastreoLote(Request.Form[0], Request.Form[1], DateTime.Now, Request.Form[2], Request.Form[3], Request.Form[4]);
                var rastreolote = dbpRepository.FindAllRastreoLote().ToList();
                return View("Agregado", rastreolote);
            }
            else
            {
                return View();
            }
            //return View();

        }

        public ActionResult Todos()
        {
            var rastreolote = dbpRepository.FindAllRastreoLote().ToList();
            return View(rastreolote);
        }

        public ActionResult Modifica()
        {
            return View(dbpRepository.GetRastreoLotes());
        }
        
        public ActionResult Modificar(int id)
        {
            RASTREO_DE_LOTE rl = dbpRepository.GetRastreoLote(id);
            return View(rl);
        }

        [HttpPost]
        public ActionResult Modificar(int id, FormCollection formValues)
        {
            RASTREO_DE_LOTE rl = dbpRepository.GetRastreoLote(id);
            if (TryUpdateModel(rl))
            {
                dbpRepository.Save();
                return RedirectToAction("Todos");
            }
            return View("Todos");
        }
    }
}
